<template>
  <div class="container">
    <h2 class="handle-title">系统管理 > 新增机构</h2>
    <el-form ref="form" :rules="rules" :model="form" class="demo-ruleForm" label-width="80px" style="width:430px">
      <el-form-item label="机构名字" prop="orgname">
        <el-input v-model="form.orgname" style="width:190px" maxlength="20"></el-input>
      </el-form-item>
      <el-form-item label="机构状态">
        <el-select v-model="form.orgauditstate" placeholder="请选择状态">
          <el-option label="可用" value="1"></el-option>
          <el-option label="不可用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机构地址">
        <el-input type="input" v-model="form.orgaddr"></el-input>
      </el-form-item>
      <el-form-item label="法定代表人">
        <el-input type="input" v-model="form.orgcontacts"></el-input>
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input type="input" v-model="form.orgphone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')" >提交</el-button>
        <el-button @click="goBack">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { addOrg } from "../../api/public/api";

export default {
  name: 'addOrg',
  data() {
    return {
      form: {
        orgname: '',
        orgauditstate: '1',
        orgaddr:'',
        orgcontacts:'',
        orgphone:''
      },
      rules: {
        rolename: [
          { required: true, message: '请输入角色名称' }
        ]
      }
    }
  },
  mounted() {
  },
  methods:{
    //新增角色
    onSubmit(formName) {
      let that = this;
      this.$refs[formName].validate((value)=>{
        // console.log(value)
        if(value){
          // console.log(that.form)
          let formData = new FormData();
          formData.append("orgname",that.form.orgname);
          formData.append("orgauditstate",that.form.orgauditstate);
          formData.append("orgaddr",that.form.orgaddr);
          formData.append("orgcontacts",that.form.orgcontacts);
          formData.append("orgphone",that.form.orgphone)


          addOrg(formData).then((res)=>{
            // console.log(res)
            if(res.data.code == 1){
              that.$message({
                message:"添加成功",
                type:"success",
              })
              that.$router.push("/organization")
            }else{
              this.$message({
                message:"添加失败",
                type:"info"
              });
            }
          })
        }
      })
    },
    //取消操作
    goBack() {
      this.$router.go(-1);
    },
  },
}
</script>

<style>
.handle-title{
  line-height: 50px;
}
.el-textarea__inner{
  height: 100px;
}
</style>
